본 게시글의 원문은 Yann Debray의 MATLAB with Python Book 입니다. 해당 책은 MIT 라이센스를 따르기 때문에 개인적으로 번역하여 재배포 합니다. 본 포스팅에는 추후 유료 수익을 위한 광고가 부착될 수도 있습니다.

MIT License

Copyright (c) 2023 Yann Debray

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

본 게시물에서 활용된 소스 코드들은 모두 Yann Debray의 GitHub Repo에서 확인할 수 있습니다.

6.3. MATLAB 커뮤니티의 작업물 활용

MATLAB은 활기차고 확립된 커뮤니티를 가지고 있습니다. 이는 사실상 Python 커뮤니티와 상호 보완적입니다. Python 커뮤니티는 더 젊고 특히 머신/딥 러닝과 관련된 분야에서 빠르게 성장하고 있습니다. MATLAB File Exchange를 통해 자신의 파일을 공유하거나 다른 사용자가 기여한 파일을 검색하고 다운로드할 수 있습니다. Git을 사용한 소스 제어가 필요하지 않지만, 원한다면 GitHub의 코드 저장소와 연결할 수 있습니다. 이를 통해 MATLAB의 추가 기능 관리자를 통해 커뮤니티 기여에 직접 접근할 수 있습니다.

이 예제에서는 날씨 데이터를 기반으로 사인 함수를 맞추기 위해 MATLAB 커뮤니티 툴박스를 사용하겠습니다.

환경 설정

!git clone https://github.com/hgorr/weather-matlab-python
# zip 파일 다운로드 및 압축 해제
url_zip = 'https://www.mathworks.com/matlabcentral/mlc-downloads/downloads/a1ca242b-82c2-4a89-b280-38d2243276da/4d399976-e76f-418a-a227-c97d2f7a85f7/packages/zip'

import requests, zipfile, io, os
r = requests.get(url_zip)
z = zipfile.ZipFile(io.BytesIO(r.content))
os.makedirs('sineFit', exist_ok=True)
z.extractall('sineFit')

날씨 데이터 가져오기

import os
os.chdir('weather-matlab-python')

뮌헨의 예보에서 샘플 데이터를 사용할 것입니다.

import weather
appid ='b1b15e88fa797225412429c1c50c122a1'
json_data = weather.get_forecast('Muenchen','DE',appid,api='samples')
data = weather.parse_forecast_json(json_data)

MATLAB 엔진은 현재 Python 인터프리터의 디렉토리에서 시작됩니다.

import matlab.engine
m = matlab.engine.connect_matlab()
m.pwd()
m.desktop(nargout=0)

데이터를 MATLAB 데이터 유형(날짜 시간 및 배정밀도 부동 소수점)으로 변환해봅시다.

dt = m.datetime(data['current_time'])
temp = matlab.double(data['temp'])
m.plot(dt,temp)
m.print(m.tempdir()+"myPlot.png",'-dpng','-r300',nargout=0)
from IPython.display import Image
Image(m.tempdir()+"myPlot.png")

노트북에서 MATLAB 플롯을 저장하는 과정을 간소화하기 위해 함수를 만들 수 있습니다.

from IPython.display import Image
def mplot(x,y):
    m.plot(x,y)
    m.print(m.tempdir()+"myPlot.png",'-dpng','-r300',nargout=0)
    return Image(m.tempdir()+"myPlot.png")
# mplot(dt,temp)

사인 곡선 피팅

# 웹페이지 https://www.mathworks.com/matlabcentral/fileexchange/66793-sine-fitting 에서 설명을 가져옵니다.
import requests
from bs4 import BeautifulSoup
from IPython.display import HTML
url = 'https://www.mathworks.com/matlabcentral/fileexchange/66793-sine-fitting'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
description = soup.find('div', id='description')#.get_text()
HTML(str(description))
m.cd('..')
m.cd('sineFit/')
t = matlab.double(range(36))
SineParams = m.sineFit(t,temp)
SineParams
m.print(m.tempdir()+"fft.png",'-dpng','-r300',nargout=0)
m.close(m.gcf()) # 현재 그림 닫기
Image(m.tempdir()+"fft.png")

m.print(m.tempdir()+"sine.png",'-dpng','-r300',nargout=0)
m.close(m.gcf())
Image(m.tempdir()+"sine.png")

사인 곡선 피팅 2: 사인 모델의 값 가져오기

주어진 매개변수 세트와 x 값 세트에 대해 사인 모델의 값을 반환하는 MATLAB 함수를 만듭니다.

m.edit('sineFit2.m',nargout=0)
function Sine=sineFit2(y)
    s = size(y);
    if s(1)>s(2)
        y = y';
    end
    n=length(y);
    x = linspace(1,n,n);
    SineP = sineFit(x,y,0); % 플롯을 생성하지 않음
    Sine = SineP(1)+SineP(2)*sin(2*pi*SineP(3)*x+SineP(4));
end
mat_temp = matlab.double(temp)
SineVal = m.sineFit2(mat_temp)[0]
SineVal
m.plot(dt,temp,dt,SineVal)
m.print(m.tempdir()+"sine2.png",'-dpng','-r300',nargout=0)
m.close(m.gcf())
Image(m.tempdir()+"sine2.png")

m.quit()

사인 곡선 피팅 2: 매개변수만 가져오기

이 두 번째 구현은 MATLAB 쪽에서 최소한의 작업을 수행하여 사인 모델의 매개변수 (a,b,c,d)를 맞춥니다:

\[a + b * \sin(2*\pi*c + d)\]
# matlab double 배열의 첫 번째 요소를 가져와 리스트로 반환해야 함
SineP = SineParams[0]
SineP
from math import sin,pi 
# SineP(1)+SineP(2)*sin(2*pi*SineP(3)*tt+SineP(4)));
def Sine(t):
    return SineP[0]+SineP[1]*sin(2*pi*SineP[2]*t+SineP[3])
# 1에서 40까지의 리스트 생성
t = list(range(1,41))
Sine(t[0])
# 파이썬에서 리스트 내포 개념을 사용하여 사인 값을 생성해야 함
SineFit = [ Sine(x) for x in t ]
SineFit
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(x=data['current_time'], y=data['temp'],
                    mode='markers',
                    name='Temperatures'))
fig.add_trace(go.Scatter(x=data['current_time'], y=SineFit,
                    mode='lines',
                    name='SineFit'))

fig.show()

이 장의 녹화된 데모는 다음에서 확인할 수 있습니다:

  • Part 1: https://github.com/yanndebray/matlab-with-python-book/assets/128002745/dc9b069e-bf73-4294-aa1d-79813df9c62d
  • Part 2: https://github.com/yanndebray/matlab-with-python-book/assets/128002745/6b777298-76ea-405e-b3ee-57fa5a18e211